﻿using Axios.Engine;
using FarseerPhysics.SamplesFramework;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Axios.Engine.Extensions;
using System;
using GameStateManagement;
using Microsoft.Xna.Framework.Input;
using Axios.Engine.UI;

namespace axios_tennis
{
#if WINDOWS
    class GameConsole : AxiosCommandConsole
    {
        public GameConsole(AxiosGameScreen gameScreen)
            : base(gameScreen)
        {

        }

        public override void InitializeCustomCommands()
        {
            base.InitializeCustomCommands();

            CmdObject reset = new CmdObject();
            reset.Command = "reset";
            reset.CommandHelp = "Resets the game";
            reset.CommandHelpDetailed = "reset";
            reset.CmdEvent += new Action<string[]>(reset_CmdEvent);
            AddCommand(reset);
        }

        void reset_CmdEvent(string[] obj)
        {
            ((TennisScreen)GameScreen).ResetGame();
        }

        protected override void LoadContent()
        {
            LoadDefault();
            base.LoadContent();
        }
    }
#endif
    class TennisScreen : AxiosGameScreen 
    {
        private Ball _ball;
        private Border _border;
        private Paddle _ppaddle;
        private Paddle _epaddle;
        private Wall _pwall;
        private Wall _ewall;
        private int _pscore = 0;
        private int _escore = 0;
        public bool Play = false;

        private SpriteFont _sf;

        public void ResetGame()
        {
            _ball.ResetBall();
            _pscore = 0;
            _escore = 0;
            _epaddle.BodyPart.Position = Vector2.Zero;
            _epaddle.PaddleJoint.MotorSpeed = 0f;
            Play = false;
            
        }

        public TennisScreen()
        {
            this.HasVirtualStick = true;
            this.HasCursor = true;
            EnableCameraControl = false;
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }

        public override void  Activate(bool instancePreserved)
        {
 	        base.Activate(instancePreserved);
            int xcount, ycount;
            Texture2D _blocks = ScreenManager.Game.Content.Load<Texture2D>("blocks");
            Texture2D[,] _blocktextures = _blocks.Split(32, 32, 8, 8, out xcount, out ycount);

#if WINDOWS_PHONE
            AxiosButton b = new AxiosButton();
            //b.AdjustUnits = false;
            b.Position = new Vector2(ConvertUnits.ToSimUnits(ScreenManager.GraphicsDevice.Viewport.Width) / 2.5f - ConvertUnits.ToSimUnits(110f),
                -ConvertUnits.ToSimUnits(ScreenManager.GraphicsDevice.Viewport.Height) / 2.5f - ConvertUnits.ToSimUnits(30f));

            //b.Position = new Vector2(1f, 1f);
            b.ClickTexture = _blocktextures[0, 0];
            b.NormalTexture = _blocktextures[0, 1];
            b.HoverTexture = _blocktextures[0, 2];
            b.Origin = new Vector2(b.ClickTexture.Width / 2, b.ClickTexture.Height / 2);
            b.MouseDown += new AxiosEvents.AxiosHandler(b_MouseDown);
            AddGameObject(b);
#endif
            _border = new Border(ScreenManager.GraphicsDevice.Viewport.Width, ScreenManager.GraphicsDevice.Viewport.Height);
            _ball = new Ball(Vector2.Zero);
            _ball.HitScoreWall += new Ball.HitScoreWallHandler(_ball_HitScoreWall);
            _ppaddle = new Paddle(new Vector2(-ConvertUnits.ToSimUnits(ScreenManager.GraphicsDevice.Viewport.Width)/2.5f, 0f));
            _epaddle = new Paddle(new Vector2(ConvertUnits.ToSimUnits(ScreenManager.GraphicsDevice.Viewport.Width)/2.5f, 0f));

            _pwall = new Wall(new Vector2(-ConvertUnits.ToSimUnits(ScreenManager.GraphicsDevice.Viewport.Width) / 2f, 0), "pwall");
            _ewall = new Wall(new Vector2(ConvertUnits.ToSimUnits(ScreenManager.GraphicsDevice.Viewport.Width) / 2f, 0), "ewall");

            _sf = ScreenManager.Game.Content.Load<SpriteFont>("scorefont");

            _ppaddle.AllowAutomaticMouseJoint = true;
            AddGameObject(_ewall);
            AddGameObject(_pwall);
            AddGameObject(_epaddle);
            AddGameObject(_ppaddle);
            AddGameObject(_border);
            AddGameObject(_ball);
#if WINDOWS
            AddGameObject(new GameConsole(this));
#endif
        }

        void b_MouseDown(object sender, AxiosGameScreen gameScreen, InputState input)
        {
            if (HasVirtualStick)
                HasVirtualStick = false;
            else
                HasVirtualStick = true;
        }

        public override void HandleInput(GameTime gameTime, InputState input)
        {

#if WINDOWS
            if (Console == null || !AxiosCommandConsole.Active || (AllowKeyboardWhileConsoleIsActive && AxiosCommandConsole.Active))
#endif
            {
                base.HandleInput(gameTime, input);


                //This must be handled in the screen because there are multiple paddles

                if (_ppaddle != null)
                {
                    if (input.VirtualState.ThumbSticks.Left.Y > 0 ||
                        input.CurrentGamePadStates[0].ThumbSticks.Left.Y > 0 ||
                        input.CurrentKeyboardStates[0].IsKeyDown(Keys.Up)
                        )
                    {
                        _ppaddle.PaddleJoint.MotorSpeed = -40f;
                    }
                    else if (input.VirtualState.ThumbSticks.Left.Y < 0 ||
                        input.CurrentGamePadStates[0].ThumbSticks.Left.Y < 0 ||
                        input.CurrentKeyboardStates[0].IsKeyDown(Keys.Down)
                        )
                    {
                        _ppaddle.PaddleJoint.MotorSpeed = 40f;
                    }
                    else
                    {
                        _ppaddle.PaddleJoint.MotorSpeed = 0f;
                    }
                }
            }
        }

        void _ball_HitScoreWall(int side)
        {
            if (side == 0)
            {
                _escore += 1;
                _ball.ConstantVelocity = new Vector2(7f, 7f);
                _ball.BodyPart.Position = Vector2.Zero;
                _ball.BodyPart.ApplyForce(new Vector2(2000f, 5000f));
            }
            else if (side == 1)
            {
                _pscore += 1;
                _ball.ConstantVelocity = new Vector2(7f, 7f);
                _ball.BodyPart.Position = Vector2.Zero;
                _ball.BodyPart.ApplyForce(new Vector2(-2000f, 5000f));
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
#if WINDOWS_PHONE
            ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
            ScreenManager.SpriteBatch.DrawString(_sf, "- Toggle Virtual Stick", new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2.5f - 80f, -ScreenManager.GraphicsDevice.Viewport.Height /2.5f - 40f), Color.Black);
            ScreenManager.SpriteBatch.End();
#endif
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (Play)
                if (_epaddle.BodyPart.Position.Y < _ball.BodyPart.Position.Y && _epaddle.PaddleJoint.MotorSpeed != 40f)
                    _epaddle.PaddleJoint.MotorSpeed += 5f;
                else if (_epaddle.BodyPart.Position.Y > _ball.BodyPart.Position.Y && _epaddle.PaddleJoint.MotorSpeed != -40f)
                    _epaddle.PaddleJoint.MotorSpeed -= 5f;
            /*else
                if (_epaddle.PaddleJoint.MotorSpeed == 40f)
                    _epaddle.PaddleJoint.MotorSpeed -= 1f;
                else if (_epaddle.PaddleJoint.MotorSpeed == -40f)
                    _epaddle.PaddleJoint.MotorSpeed += 1f;*/

        }
    }
}
